package com.chatroomserver.controller;

import com.chatroomserver.annotation.Userid;
import com.chatroomserver.entity.Notice;
import com.chatroomserver.service.NoticeService;
import com.chatroomserver.utils.ResultUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    NoticeService noticeService;

    // 获取最新一条公告
    @GetMapping("/latest")
    public Object getLatestNotice() {
        Notice notice = noticeService.getLatestNotice();
        return ResultUtil.Succeed(notice);
    }

    // 创建公告
    @PostMapping("/create")
    public Object createNotice(@Userid String userId, @RequestBody Map<String, String> payload) {
        String content = payload.get("content");
        boolean result = noticeService.createNotice(userId, content);
        return ResultUtil.ResultByFlag(result);
    }

}
